package org.example.service;

import org.example.model.MaterialRequest;
import org.example.repository.MaterialRequestDao;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MaterialRequestService {

    private final MaterialRequestDao materialRequestDao;

    public MaterialRequestService(MaterialRequestDao materialRequestDao) {
        this.materialRequestDao = materialRequestDao;
    }

    // 添加物料申请
    public int addMaterialRequest(MaterialRequest request) {
        return materialRequestDao.insertMaterialRequest(request);
    }

    // 修改物料申请
    public int updateMaterialRequest(MaterialRequest request) {
        return materialRequestDao.updateMaterialRequest(request);
    }

    // 查询所有物料申请
    public List<MaterialRequest> getAllMaterialRequests() {
        return materialRequestDao.getAllMaterialRequests();
    }

    // 根据ID查询物料申请
    public MaterialRequest getMaterialRequestById(Integer requestId) {
        return materialRequestDao.getMaterialRequestById(requestId);
    }

    // 删除物料申请
    public void deleteMaterialRequest(Integer requestId) {
        materialRequestDao.deleteMaterialRequest(requestId);
    }

    /**
     * 按条件分页查询物料申请信息
     * @param materialName 物料名称（模糊查询）
     * @param approvalStatus 审批状态（精确匹配）
     * @param pageable 分页参数
     * @return 分页后的物料申请信息
     */
    public Page<MaterialRequest> searchMaterialRequests(
            String materialName,
            String approvalStatus,
            Pageable pageable) {
        // 查询总记录数
        int totalCount = materialRequestDao.countSearchMaterialRequests(materialName, approvalStatus);
        // 查询当前页数据
        List<MaterialRequest> list = materialRequestDao.searchMaterialRequests(materialName, approvalStatus, pageable);
        // 返回分页结果
        return new PageImpl<>(list, pageable, totalCount);
    }
}